﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Text;
using System.IO;

namespace OA.Controllers
{
    public class CardTemplateDesignMainController : Controller
    {
        //
        // GET: /CardTemplateDesignMain/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CardTemplateDesignMain()
        {
            
           
            return View();

        }
        //获取模板名
        public ActionResult getTepName()
        {
            cardTemplates CT = new cardTemplates();
            cardTemplatesManager CTManger = new cardTemplatesManager();
            List<string> Names = CTManger.Get_Name().ToList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Names.Count(); i++)
            {
                sb.Append("<li class=\"tep_lis\" onmouseover=\"tep_Over(this)\" onmouseout=\"tep_Out(this)\" onclick=\"tep_Click(this)\">" + Names[i] + "</li>");
            }
            return Content(sb.ToString());
        }

        public ActionResult getTepByTepName(FormCollection fc)
        {
            string tepName = fc["tepName"];
            cardTemplates CT = new cardTemplates();
            cardTemplatesManager CTManger = new cardTemplatesManager();
            cardTemplates tep=  CTManger.getByTepName(tepName);
           // string[] items = tep.template.Split('\n');
            string item = tep.template.ToString();
              
            return Content(item);
        }
        //添加模板
        public ActionResult Save_tep( FormCollection fc)
        {
            string tep_name = fc["tep_name"];
            string itemTep = fc["itemTep"];
            cardTemplates CT = new cardTemplates();
            cardTemplatesManager CTManger = new cardTemplatesManager();
            CT.name = tep_name;
            CT.template = itemTep;
            CTManger.Add(CT);
            return Content("添加模板成功");
             
        }
        //更新模板
        public ActionResult Update_tep(FormCollection fc)
        {
            string tep_name = fc["tep_name"];
            string itemTep = fc["itemTep"];
            cardTemplates CT = new cardTemplates();
            cardTemplatesManager CTManger = new cardTemplatesManager();
            cardTemplates tep = CTManger.getByTepName(tep_name);
            tep.name = tep_name;
            tep.template = itemTep;
            CTManger.Update();
            return Content("更新模板成功");

        }
        public ActionResult inputPic()
        {
            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            string physicalPath = "";
            string Vpath = "";
            if (hfc.Count > 0)
            {
                string StrEx = Path.GetExtension(hfc[0].FileName).ToLower();
                if (StrEx == ".jpg" || StrEx == ".png")
                {
                    string path = "~/Content/SelfAdd/" +"tep_backgd"+StrEx;
                    physicalPath = Server.MapPath(path);
                    System.IO.File.Delete(physicalPath);
                    hfc[0].SaveAs(physicalPath);
                    Vpath = "../../Content/SelfAdd/" + "tep_backgd" + StrEx;
                }
                else
                {
                    return Content("图片格式不正确");
                }
            }
            return Content(Vpath);
        }
    }
}
